import Vue from "vue";
import App from "./App.vue";
import router from "./router";

// 引入全局css
import "./assets/scss/style.scss";
// 引入Element UI
import Element from "element-ui";
// import "element-ui/lib/theme-chalk/index.css";
//自定义主题
import "./assets/theme/index.css";
// 引入iconfont图标
import "@/assets/icon/iconfont.css";
// 引入dataV
import dataV from "@jiaminghi/data-view";
// 引入echart
import * as echarts from "echarts";
// 引入axios
import axios from "axios";

// 全局注册
Vue.use(Element);
Vue.use(dataV);
Vue.prototype.$echarts = echarts;
Vue.prototype.$axios = axios;

// 登录用户是否为管理员
import personalApi from "@/api/personal.js";
async function getUser() {
  const login_user = await personalApi.getInfo();
  return login_user.data.user_role == 1;
}
let user = null;
user == null && window.localStorage.getItem("token") != null ? (user = getUser()) : null;

// 路由守卫
import { addAdminRoutes } from "@/router/index.js";
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  if (to.path == "/login") {
    next();
  } else {
    let token = window.localStorage.getItem("token");
    if (token == null || token == "") {
      next("/login");
    } else {
      if (!router.options.isAddAsyncRoute && user) {
        addAdminRoutes();
        next({ ...to });
      } else next();
    }
  }
});

Vue.config.productionTip = false;

new Vue({
  router,
  render: (h) => h(App),
}).$mount("#app");
